﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using DAL.SystemDAL;
using Model.SystemManage;
using System.Data;

namespace BLL.SystemBLL
{
    public class EmployeeInfo_Bll 
    {
        EmployeeInfo_Dal dal = new EmployeeInfo_Dal();
        /// <summary>
        /// 员工登录
        /// </summary>
        /// <returns></returns>
        public EmployeeLoginInfo_Model IsLogin(string Account, string Password)
        {
            return dal.IsLogin(Account, Password);
        }

        /// <summary>
        /// 判断登录后的用户是否有完整的基础信息
        /// </summary>
        /// <param name="EmployID">员工ID</param>
        /// <returns></returns>
        public EmployeeBasicInfo_Model GetIsBasicInfo(int EmployID)
        {
            return dal.GetIsBasicInfo(EmployID);
        }

        /// <summary>
        /// 获取政治面貌列表信息
        /// </summary>
        /// <returns></returns>
        public string GetPoliticalList()
        {
            return dal.GetPoliticalList();
        }

        /// <summary>
        /// 获取学历列表信息
        /// </summary>
        /// <returns></returns>
        public string GetEducationList()
        {
            return dal.GetEducationList();
        }

        /// <summary>
        /// 获取学位信息
        /// </summary>
        /// <returns></returns>
        public string GetPositionList()
        {
            return dal.GetPositionList();
        }

        /// <summary>
        /// 添加员工基本信息
        /// </summary>
        /// <param name="basic"></param>
        /// <returns></returns>
        public int InsertBasicEmployeeInfo(EmployeeBasicInfo_Model basic)
        {
            return dal.InsertBasicEmployeeInfo(basic);
        }

        /// <summary>
        /// 删除个人基础信息
        /// </summary>
        /// <param name="EmployeeId">员工ID</param>
        /// <returns></returns>
        public int DeleteBasicEmployeeInfoById(int EmployeeId)
        {
            return dal.DeleteBasicEmployeeInfoById(EmployeeId);
        }

        /// <summary>
        /// 通过员工ID获取员工基础信息
        /// </summary>
        /// <param name="EmployeeID"></param>
        /// <returns></returns>
        public EmployeeBasicInfo_Model GetBasicEmployeeInfoById(int EmployeeID)
        {
            return dal.GetBasicEmployeeInfoById(EmployeeID);
        }

        /// <summary>
        /// 获取权限信息
        /// </summary>
        /// <returns></returns>
        public List<LimitInfo_Model> GetLimitInfo(int DeptID, int PositionID)
        {
            return dal.GetLimitInfo(DeptID,PositionID);
        }

        /// <summary>
        /// 显示部门/角色信息，支持模糊查询
        /// </summary>
        /// <param name="DeptName">角色名称</param>
        /// <returns></returns>
        public List<Dept_Model> GetRoleList(string DeptName)
        {
            return dal.GetRoleList(DeptName);
        }

        /// <summary>
        /// 添加部门/角色信息
        /// </summary>
        /// <param name="dept">部门实体类传输</param>
        /// <returns></returns>
        public int InsertDeptInfo(Dept_Model dept)
        {
            return dal.InsertDeptInfo(dept);
        }

        /// <summary>
        /// 员工信息显示
        /// </summary>
        /// <returns></returns>
        public List<EmployeeBasicInfo_Model> GetEmployeeBasicInfo()
        {
            return dal.GetEmployeeBasicInfo();
        }

        /// <summary>
        /// 分页/显示员工信息表
        /// </summary>
        /// <param name="PageIndex">当前索引</param>
        /// <param name="PageSize">每页多少条</param>
        /// <param name="count">记录总数量</param>
        /// <returns></returns>
        public List<EmployeeBasicInfo_Model> GetEmployeeBasicInfo(int PageIndex, int PageSize, out int count, int EmployeeID = 0)
        {
            return dal.GetEmployeeBasicInfo(PageIndex, PageSize, out count,EmployeeID);
        }

        /// <summary>
        /// 分页/显示员工信息表
        /// </summary>
        /// <param name="PageIndex">当前索引</param>
        /// <param name="PageSize">每页多少条</param>
        /// <param name="count">记录总数量</param>
        /// <param name="EmployeeID">员工姓名</param>
        /// <returns></returns>
        public List<EmployeeBasicInfo_Model> GetEmployeeBasicInfo(int PageIndex, int PageSize, out int count, string EmployName)
        {
            return dal.GetEmployeeBasicInfo(PageIndex, PageSize, out count, EmployName);
        }

        /// <summary>
        /// 执行用户角色分配
        /// </summary>
        /// <param name="EmpId"></param>
        /// <param name="DeptID"></param>
        /// <param name="PositionID"></param>
        /// <returns></returns>
        public int SaveEmpDeptAllot(int EmpId, int DeptID, int PositionID)
        {
            if (DeptID == 0)
            {
                return dal.UpdatePosition(EmpId, PositionID);
            }
            else
            {
                return dal.UpdateDept(EmpId, DeptID);
            }
        }

        /// <summary>
        /// 为部门/职位分配权限
        /// </summary>
        /// <param name="DeptId">部门id</param>
        /// <param name="PositionId">职位id</param>
        /// <param name="limitIds">权限id</param>
        /// <returns></returns>
        public int GiveDeptAllotLimit(int DeptId, int PositionId, string limitIds)
        {
            //根据部门id ，职位id，将其所拥有的权限，删除掉
            dal.DelLimitInfoByDeptIdAndPositionId(DeptId, PositionId);

            //分局逗号分隔成string数组
            string[] limits = limitIds.Split(',');
            //拼接sql语句
            StringBuilder sql = new StringBuilder();

            //拼接sql语句
            for (int i = 0; i < limits.Length; i++)
            {
                sql.Append("insert into DeptConnLimit_Tab values(" + DeptId + "," + limits[i] + "," + PositionId + ");");
            }

            return dal.GiveDeptAllotLimit(sql.ToString());
        }

        /// <summary>
        /// 系统管理员注册员工
        /// </summary>
        /// <param name="register"></param>
        /// <returns></returns>
        public int RegisterEmployee(EmployeeLoginInfo_Model register)
        {
            return dal.RegisterEmployee(register);
        }

        /// <summary>
        /// 修改员工基础信息审核状态
        /// </summary>
        /// <param name="InfoState">信息状态</param>
        /// <param name="EmpId">员工Id</param>
        /// <returns></returns>
        public int UpdateBasicInfoState(int InfoState, int EmpId)
        {
            return dal.UpdateBasicInfoState(InfoState, EmpId);
        }

        /// <summary>
        /// 修改员工当前账号状态
        /// </summary>
        /// <param name="InfoState">员工状态</param>
        /// <param name="EmpId">员工Id</param>
        /// <returns></returns>
        public int UpdateLoginInfoState(int PersonState, int EmpId)
        {
            return dal.UpdateLoginInfoState(PersonState, EmpId);
        }

        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <param name="EmpId">员工Id</param>
        /// <param name="newpass">新密码</param>
        /// <returns></returns>
        public string EditPassword(int EmpId, string newpass)
        {
            return dal.EditPassword(EmpId, newpass);
        }
    }
}
